ARD2  1.00 for Rev B. Hardware
Airbag Reference Demonstrator using MPC5604P
Miranda.h
Go to the documentation of this file.
00001 
00017 #ifndef _MIRANDA_H
00018 #define _MIRANDA_H
00019  
00020 /*
00021  **************************************************************
00022  * Defines, Macros and Typedefs 
00023  **************************************************************/
00024 /*** Constant Macros ***/
00025 /* Default Yes and No defines */
00026 #ifndef TRUE
00027  #define TRUE (1u)
00028 #endif
00029 #ifndef CLEAR
00030  #define CLEAR (0u)
00031 #endif
00032 #ifndef BITS_IN_NIBBLE
00033  #define BITS_IN_NIBBLE (4u)
00034 #endif
00035 #ifndef BITS_IN_BYTE
00036  #define BITS_IN_BYTE (8u)
00037 #endif
00038 #ifndef BITS_IN_16
00039  #define BITS_IN_16 (16u)
00040 #endif
00041 #ifndef BYTES_IN_16
00042  #define BYTES_IN_16  (2u)
00043 #endif
00044 #ifndef BYTES_IN_32
00045  #define BYTES_IN_32  (4u)
00046 #endif
00047 #ifndef BIT_DEFINITION
00048  #define BIT_DEFINITION
00049  #define BIT0  (1u << 0u)
00050  #define BIT1  (1u << 1u)
00051  #define BIT2  (1u << 2u)
00052  #define BIT3  (1u << 3u)
00053  #define BIT4  (1u << 4u)
00054  #define BIT5  (1u << 5u)
00055  #define BIT6  (1u << 6u)
00056  #define BIT7  (1u << 7u)
00057  #define BIT8  (1u << 8u)
00058  #define BIT9  (1u << 9u)
00059  #define BIT10 (1u << 10)
00060  #define BIT11 (1u << 11)
00061  #define BIT12 (1u << 12)
00062  #define BIT13 (1u << 13)
00063  #define BIT14 (1u << 14)
00064  #define BIT15 (1u << 15)
00065  #define BIT16 (1u << 16)
00066  #define BIT17 (1u << 17)
00067  #define BIT18 (1u << 18)
00068  #define BIT19 (1u << 19)
00069  #define BIT20 (1u << 20)
00070  #define BIT21 (1u << 21)
00071  #define BIT22 (1u << 22)
00072  #define BIT23 (1u << 23)
00073  #define BIT24 (1u << 24)
00074  #define BIT25 (1u << 25)
00075  #define BIT26 (1u << 26)
00076  #define BIT27 (1u << 27)
00077  #define BIT28 (1u << 28)
00078  #define BIT29 (1u << 29)
00079  #define BIT30 (1u << 30)
00080  #define BIT31 (1u << 31)
00081 #endif
00082 
00083 
00084 /* Register Addresses for the device - Protocol 0 and Protocol 1 */
00085 /* (5 because MOSI bit 20 is ignored by Miranda) */
00086 #define MIRANDA_MEMORY_MAP_SIZE      (0x2Eu)
00087 #define MIRANDA_SN_0                 (0x00u << 5u)
00088 #define MIRANDA_SN_1                 (0x02u << 5u)
00089 #define MIRANDA_ID_0                 (0x04u << 5u)
00090 #define MIRANDA_ID_1                 (0x06u << 5u)
00091 #define MIRANDA_DEVCFG_0             (0x08u << 5u)
00092 #define MIRANDA_DEVCFG_1             (0x0Au << 5u)
00093 #define MIRANDA_CD_0C                (0x0Cu << 5u)
00094 #define MIRANDA_CD_0E                (0x0Eu << 5u)
00095 #define MIRANDA_CD_10                (0x10u << 5u)
00096 #define MIRANDA_CD_12                (0x12u << 5u)
00097 #define MIRANDA_CD_14                (0x14u << 5u)
00098 #define MIRANDA_CD_16                (0x16u << 5u)
00099 #define MIRANDA_CD_18                (0x18u << 5u)
00100 #define MIRANDA_CD_1A                (0x1Au << 5u)
00101 #define MIRANDA_CD_1C                (0x1Cu << 5u)
00102 #define MIRANDA_CD_1E                (0x1Eu << 5u)
00103 #define MIRANDA_DEVSTAT              (0x20u << 5u)
00104 #define MIRANDA_DEVCTL               (0x22u << 5u)
00105 #define MIRANDA_TEST                 (0x24u << 5u)
00106 #define MIRANDA_RATE_X               (0x26u << 5u)
00107 #define MIRANDA_ACC_Y                (0x28u << 5u)
00108 #define MIRANDA_ACC_Z                (0x2Au << 5u)
00109 #define MIRANDA_TEMP                 (0x2Cu << 5u)
00110 #define MIRANDA_COUNT                (0x2Eu << 5u)
00111 
00112 /* Register Addresses for the device - Protocol 2 */
00113 #define MIRANDA_MEMORY_MAP_SIZE_P2    (0x1Fu)
00114 #define MIRANDA_RATE_X_P2             (0x01u << 10u)
00115 #define MIRANDA_ACC_Y_P2              (0x05u << 10u)
00116 #define MIRANDA_ACC_Z_P2              (0x06u << 10u)
00117 #define MIRANDA_TEMP_P2               (0x07u << 10u)
00118 #define MIRANDA_BANK_P2               (0x08u << 10u)
00119 #define MIRANDA_TEST_P2               (0x15u << 10u)
00120 #define MIRANDA_RESET_P2              (0x16u << 10u)
00121 #define MIRANDA_SENS_SYNC_P2          (0x18u << 10u)
00122 #define MIRANDA_DEVSTAT_P2            (0x19u << 10u)
00123 #define MIRANDA_DEVCTL_P2             (0x1Au << 10u)
00124 #define MIRANDA_COUNT_P2              (0x1Bu << 10u)
00125 #define MIRANDA_DEV_ID_P2             (0x1Du << 10u)
00126 #define MIRANDA_SN_0_P2               (0x10u << 10u)
00127 #define MIRANDA_SN_1_P2               (0x11u << 10u)
00128 #define MIRANDA_ID_0_P2               (0x12u << 10u)
00129 #define MIRANDA_ID_1_P2               (0x13u << 10u)
00130 #define MIRANDA_DEVCFG_0_P2           (0x14u << 10u)
00131 #define MIRANDA_DEVCFG_1_P2           (0x15u << 10u)
00132 #define MIRANDA_CD_0C_P2              (0x16u << 10u)
00133 #define MIRANDA_CD_0E_P2              (0x17u << 10u)
00134 #define MIRANDA_CD_10_P2              (0x18u << 10u)
00135 #define MIRANDA_CD_12_P2              (0x19u << 10u)
00136 #define MIRANDA_CD_14_P2              (0x1Au << 10u)
00137 #define MIRANDA_CD_16_P2              (0x1Bu << 10u)
00138 #define MIRANDA_CD_18_P2              (0x1Cu << 10u)
00139 #define MIRANDA_CD_1A_P2              (0x1Du << 10u)
00140 #define MIRANDA_CD_1C_P2              (0x1Eu << 10u)
00141 #define MIRANDA_CD_1E_P2              (0x1Fu << 10u)
00142 
00143 /* Register related */
00144 #define MIRANDA_READ_REGISTER    (0x00u)   
00145 #define MIRANDA_WRITE_REGISTER   (0x80u)
00146 
00147 /* Inertial reading settings */
00148 #define MIRANDA_READ_X          (0x0001u)
00149 #define MIRANDA_READ_Y          (0x0002u)
00150 #define MIRANDA_READ_Z          (0x0003u)
00151 
00152 /* Inertial data request */
00153 #define MIRANDA_READ_ACCEL       (0x2000u)
00154 
00155 /* Error bits */
00156 #define MIRANDA_INVALID_PARITY          (0x10u)
00157 #define MIRANDA_TIMED_OUT               (0x20u)
00158 #define MIRANDA_INVALID_MEMORY_LOCATION (0x40u)
00159 #define MIRANDA_ERROR_PROTOCOL          (0x80u)
00160 #define MIRANDA_EXCEPTION               (0x11u)
00161 #define MIRANDA_INVALID_CRC             (0x12u)
00162 
00163 /* Exception Mask */
00164 #define MIRANDA_EXCEPTION_MASK          (0x07u)
00165 #define MIRANDA_INTERNAL_ERROR          (0x03u)
00166 #define MIRANDA_SPI_FAULT               (0x05u)
00167 #define MIRANDA_MISO_COMPARE_FAULT      (0x06u)
00168 
00169 /* To filter out inertial data */
00170 #define MIRANDA_RAW_INERTIAL_MASK_UPPER          (0x03FFu)
00171 #define MIRANDA_RAW_INERTIAL_MASK_LOWER          (0xFC00u)
00172 
00173 /* Communication protocol */
00174 #define PROTOCOL_0   0
00175 #define PROTOCOL_1   1
00176 #define PROTOCOL_2   2
00177 
00178 /*** Function Macros ***/
00179 #ifndef N_ELEMENTS
00180  #define N_ELEMENTS(X)           (sizeof(X)/sizeof(*(X)))
00181 #endif
00182 
00183 /*** Enums ***/
00184 
00185 /*** TypeDefs ***/
00186 
00187 /*
00188  **************************************************************
00189  * Declarations 
00190  **************************************************************/
00191 /*** Extern ***/
00192 extern const uint16_t cau16MirandaReadAllRegisters[];
00193 extern const uint16_t cau16MirandaReset[];
00194 extern const uint16_t cau16MirandaPerformAccelReading[];
00195 extern const uint16_t cau16MirandaPerformAccelReadingSPI[];
00196 /*** Globals ***/
00197 
00198 /*** Static Globals ***/
00199 
00200 /*
00201  **************************************************************
00202  * Function Prototypes 
00203  **************************************************************/
00204 /*
00205  ******************************************************************************
00206  *
00207  * Function:          u8fnMirandaReadRegister()
00208  *
00209  */
00222 uint8_t u8fnMirandaReadRegister(const uint8_t cu8DSPIInstance, \
00223                                 const uint8_t cu8ChipSelect,   \
00224                                 uint16_t u16Register,          \
00225                                 uint16_t* pu16DataRead,        \
00226                                 const uint8_t cu8Protocol);
00227 /*
00228  ******************************************************************************
00229  *
00230  * Function:          u8fnMirandaWriteRegister()
00231  *
00232  */
00246 uint8_t u8fnMirandaWriteRegister(const uint8_t cu8DSPIInstance, \
00247                                  const uint8_t cu8ChipSelect,   \
00248                                  uint16_t u16Register,          \
00249                                  uint16_t u16ValueToWrite,      \
00250                                  uint16_t* pu16DataRead,        \
00251                                  const uint8_t cu8Protocol);
00252 /*
00253  ******************************************************************************
00254  *
00255  * Function:          u8fnMirandaReset()
00256  *
00257  */
00268 uint8_t u8fnMirandaReset(const uint8_t cu8DSPIInstance, \
00269                          const uint8_t cu8ChipSelect,   \
00270                          const uint8_t cu8Protocol);
00271 /*
00272  ******************************************************************************
00273  *
00274  * Function:        u8fnMirandaReadAccelRegisters()
00275  *
00276  */
00287 uint8_t u8fnMirandaReadAccelRegisters(const uint8_t cu8DSPIInstance, \
00288                                       const uint8_t cu8ChipSelect,   \
00289                                       const uint8_t cu8Protocol);
00290 /*
00291  ******************************************************************************
00292  *
00293  * Function:        u8fnMirandaReadAllRegisters()
00294  *
00295  */
00306 uint8_t u8fnMirandaReadAllRegisters(const uint8_t cu8DSPIInstance, \
00307                                     const uint8_t cu8ChipSelect,   \
00308                                     const uint8_t cu8Protocol);
00309 /*
00310  ******************************************************************************
00311  *
00312  * Function:          u8fnMirandaReadAccelSPI()
00313  *
00314  */
00327 uint8_t u8fnMirandaReadAccelSPI(const uint8_t cu8DSPIInstance, \
00328                                 const uint8_t cu8ChipSelect, 
00329                                 const uint8_t cu8Axis,
00330                                 uint16_t* pu16AccelResults);
00331 /*
00332  ******************************************************************************
00333  *
00334  * Function:          u8fnMirandaBatchOp()
00335  *
00336  */
00352 uint8_t u8fnMirandaBatchOp(const uint8_t cu8DSPIInstance,        \
00353                            const uint8_t cu8ChipSelect,          \
00354                            const uint16_t* pu16DataToSend,       \
00355                            uint16_t* pu16UnfilteredDataReceived, \
00356                            uint8_t u8Size,                       \
00357                            const uint8_t cu8Protocol);
00358 /*
00359  ******************************************************************************
00360  *
00361  * Function:          u8fnMMA6800ExtractRegisterResponse()
00362  *
00363  */
00374 uint8_t u8fnMirandaExtractRegisterResponse(const uint16_t* pu16RawResponse, \
00375                                            uint16_t* pu16Response,            \
00376                                            uint8_t u8Size,                  \
00377                                            const uint8_t cu8Protocol);
00378 /*
00379  ******************************************************************************
00380  *
00381  * Function:          u8fnMirandaExtractAccelResponse()
00382  *
00383  */
00393 uint8_t u8fnMirandaExtractAccelResponse(const uint16_t* pu16RawResponse, \
00394                                         uint16_t* pu16Response,          \
00395                                         uint8_t u8Size);
00396 /*
00397  ******************************************************************************
00398  *
00399  * Function:          u8fnMirandaRegisterOperation()
00400  *
00401  */
00416 static uint8_t u8fnMirandaRegisterOperation(const uint8_t u8RegisterOperation,\
00417                                             const uint8_t cu8DSPIInstance,    \
00418                                             const uint8_t cu8ChipSelect,      \
00419                                             uint16_t u16Register,             \
00420                                             const uint16_t u16ValueToWrite,   \
00421                                             uint16_t* pu16DataRead,           \
00422                                             const uint8_t cu8Protocol);
00423 /*
00424  ******************************************************************************
00425  *
00426  * Function:          u8fnMirandaErrorResponseAnalysis()
00427  *
00428  */
00437 static uint8_t u8fnMirandaErrorResponseAnalysis(const uint32_t cu32ReceivedWord,\
00438                                                         const uint8_t cu8Protocol);
00439 /*
00440  ******************************************************************************
00441  *
00442  * Function:          u16fnMirandaAddParityToCommand()
00443  *
00444  */
00452 static uint16_t u16fnMirandaAddParityToCommand(uint32_t u32RawCommand);
00453 #endif /* _FILENAME_H */